package action;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import entity.Students;
import service.StudentsDAO;
import service.impl.StudentsDAOImpl;

public class StudentsAction extends SuperAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public String query(){
		StudentsDAO sdao = new StudentsDAOImpl();
		List<Students> list = sdao.queryAllStudetns();
		
		if(list != null && list.size() > 0){
			session.setAttribute("students_list", list);
		}
		return "query_success";
	}
	
	public String delete(){
		StudentsDAO sdao = new StudentsDAOImpl();
		String sid = request.getParameter("sid");
		sdao.deleteStudents(sid);
		return "delete_success";
	}
	
	public String add() throws ParseException{
		StudentsDAO sdao = new StudentsDAOImpl();
		Students student = new Students();
		student.setSname(request.getParameter("sname"));
		student.setGender(request.getParameter("gender"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
		student.setBirthday(sdf.parse(request.getParameter("birthday")));
		student.setAddress(request.getParameter("address"));
		sdao.addStudents(student);
		return "add_success";
	}
	
	public String modify(){
		 String sid = request.getParameter("sid");
		 StudentsDAO sdao = new StudentsDAOImpl();
		 Students student = sdao.queryStudentsBySid(sid);
		 session.setAttribute("modify_students", student);
		 return "modify_success";
	}
	
	public String save() throws ParseException{
		StudentsDAO sdao = new StudentsDAOImpl();
		Students student = new Students();
		student.setSid(request.getParameter("sid")); 
		student.setSname(request.getParameter("sname"));
		student.setGender(request.getParameter("gender"));
		
		SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
		student.setBirthday(sdf.parse(request.getParameter("birthday")));
		student.setAddress(request.getParameter("address"));
		sdao.updateStudents(student);
		return "save_success";
	}
}
